// index.js
const app = getApp();
Page({
  data: {
    upArray: ['110','120','130','140','150','160'],
    downArray: ['60','70','80','90','100','110'],
    upValue: [3],
    downValue: [2],
    statusString: "",
    statusImg: "../../images/status56.png",
    isShowMask:false,
    zySpeedIndex: 0,
    fySpeedIndex: 0,
    // 触摸开始时间
    touchStartTime: 0,
    // 触摸结束时间
    touchEndTime: 0,
    spd: "0" ,
    err: "0",
    spoiler: "0",
    mode: "0",
    m1: "0",
    m2: "0",
    m1max: "0",
    m2max: "0",
    vol: "0",
    isBleConnect: false,
    isSport:false,
    isGame:false
  },

  onLoad() {
    let that = this;
//     setInterval(function () {
//       //  需要执行的代码
//       that.getDataBlock(null);
//   }, 1000); // 2000为毫秒级参数，表示2秒
    // that.setData({
    //   mode: that.getModeEumn()
    // })

    that.setData({
      mode: that.getModeEumn()
    });
  },

  getModeEumn(mode) {
    switch (parseInt(mode)) {
      case 0:
        return "自学习";
      case 1:
        return "自动";
      case 2:
        return "手动";
      case 3:
        return "故障";
      case 4:
        return "调试";
      case 5:
        return "";
      case 6:
        return "娱乐";
      default:
        break;
    }
  },

  getStatusEumn(status){
    let that = this;
    var statusString = "";
    var statusImg  = "";
    switch (parseInt(status)) {
      case 0:
        statusString = "上电初始化";
        statusImg  = "../../images/status0.png";
        break;
      case 1:
        statusString = "运行到零点";
        statusImg  = "../../images/status1.png";
      break;  
      case 2:
        statusString = "升到1档";
        statusImg  = "../../images/status2.png";
      break;  
      case 3:
        statusString = "1档升到2档";
        statusImg  = "../../images/status3.png";
      break;  
      case 4:
        statusString = "2档降到1档";
        statusImg  = "../../images/status4.png";
      break;  
      case 7:
        statusString = "故障";
        statusImg  = "../../images/status7.png";
      break;  
      case 8:
        statusString = "零点";
        statusImg  = "../../images/status8.png";
      break;  
      case 9:
        statusString = "1档";
        statusImg  = "../../images/status9.png";
      break;  
      case 10:
        statusString = "2档";
        statusImg  = "../../images/status10.png";
      break; 
      default:
        statusString = "";
        statusImg  = "../../images/status56.png";
        break;
    }
  that.setData({
    statusString: statusString,
    statusImg: statusImg
  })
  },
  
  goList() {
    let that = this;
    that.setData({
      isSport: false
    })
    wx.navigateTo({
      url: '../equipmentList/equipmentList'
    })
  },

  showMask() {
    let that = this;
    if(that.judgeBleConnect()){
      that.setData({
        isShowMask: true,
        isSport: true
      })
    }
  },

  //点击娱乐模式
  changeGameMode() {
    let that = this;
    // that.setData({
    //     isGame: !that.data.isGame
    // })
    if(that.judgeBleConnect()){
      var order = "cmd:key=12\r\n";
      app.globalData.bleTool.writeBLECharacteristicValue(order);
    }
  },

  cancel(){
    let that = this;
    that.setData({
        isShowMask: false
    })
    var order = "cmd:key=1\r\n";
    app.globalData.bleTool.writeBLECharacteristicValue(order);
    console.log("点击了取消");
  },

  complete(){
    let that = this;
    that.setData({
      isShowMask: false
    })
    var zySpeedArr = ["30","60","90"];
    var fySpeedArr = ["60","90","120"];
    var zySpeed = zySpeedArr[that.data.zySpeedIndex];
    var fySpeed = fySpeedArr[that.data.fySpeedIndex];
    // var order = "cmd:spd1=" + zySpeed + ",spd2=" + fySpeed + ",key=1\r\n";
    var order = "cmd:spd1=" + zySpeed + ",spd2=" + fySpeed + "\r\n";
    app.globalData.bleTool.writeBLECharacteristicValue(order);
    app.globalData.bleTool.writeBLECharacteristicValue("cmd:key=1\r\n");
    // app.globalData.bleTool.writeBLECharacteristicValue("cmd:spd1=60\r\n");

  },


  getDataBlock(data){
    let that = this;
    // console.log("index 页面获取到了数据:" + data);
    // var data = "status:spd=0,err=1,spoiler=1,mode=0,m1=0,m2=0,m1max=0,m2max=0,vol=12.1,dbg=0123456789";
    var dataStr = data.split(":")[1];
    var dataInfo = dataStr.split(",");
    console.log(dataInfo);
    var spd = "0";
    var err = "0";
    var spoiler = "0";
    var mode = "0";
    var m1 = "0";
    var m2 = "0";
    var m1max = "0";
    var m2max = "0";
    var vol = "0";
    var dbg = "0";
    for(var i = 0;i < dataInfo.length; i++){
        var item = dataInfo[i].split("=");
        var value = item[1];
        switch (i) {
            case 0:
                spd = value;
                break;
            case 1:
                err = value;
                break;
            case 2:
                spoiler = value;
                break;
            case 3:
                mode = value;
                break;
            case 4:
                m1 = value;
                break;    
            case 5:
                m2 = value;
                break;
            case 6:
                m1max = value;
                break;
            case 7:
                m2max = value;
                break;
            case 8:
              vol = value;
              break;   
            case 9:
              dbg = value;
              break;
            default:
              break;
        }
    }
    that.setData({
        spd: spd,
        err: err,
        spoiler: spoiler,
        mode: mode,
        m1: m1,
        m2: m2,
        m1max: m1max,
        m2max: m2max,
        vol: vol
    });
    that.getStatusEumn(that.data.spoiler);
// console.log(vol);
  },

  reset(){
    let that = this;
    console.log("reset");
    that.setData({
      isSport: false
    })
    if(that.judgeBleConnect()){
    // this.showModal();
    wx.showModal({
      title: '提示',
      content: '是否重置所有设置',
      confirmText: '确定',
      cancelText: '取消',
      cancelColor: '#EE2C2C',
      success: function(res){
        if(res.confirm){
          var order = "cmd:key=0\r\n";
          app.globalData.bleTool.writeBLECharacteristicValue(order);
        }
      }
    })
    }
  },

  setUpOne() {
    let that = this;
    that.setData({
      isSport: false
    })
    if(that.judgeBleConnect()){
      var order = "cmd:key=2\r\n";
      app.globalData.bleTool.writeBLECharacteristicValue(order);
    }
  },

  setUpTwo() {
    let that = this;
    that.setData({
      isSport: false
    })
    if(that.judgeBleConnect()){
      var order = "cmd:key=3\r\n";
      app.globalData.bleTool.writeBLECharacteristicValue(order);
    }
  },

  setDownOne() {
    let that = this;
    that.setData({
      isSport: false
    })
    if(that.judgeBleConnect()){
      var order = "cmd:key=4\r\n";
      app.globalData.bleTool.writeBLECharacteristicValue(order);
    }
  },

  setDownTwo() {
    let that = this;
    that.setData({
      isSport: false
    })
    if(that.judgeBleConnect()){
      var order = "cmd:key=5\r\n";
      app.globalData.bleTool.writeBLECharacteristicValue(order);
    }
  },

  changeZy(e){
    var index = e.currentTarget.dataset.index;
    let that = this;
    var fySpeedIndex = that.data.fySpeedIndex;
    var changeFySpeedIndex = that.data.fySpeedIndex; 
    switch (index) {
      case 0:
        break;
      case 1:
        if(fySpeedIndex == 0){
          changeFySpeedIndex = 1;
        }
        break;
      case 2:
       if(fySpeedIndex <= 1 ){
        changeFySpeedIndex = 2;
       }
        break;
      default:
        break;
    }
    that.setData({
      fySpeedIndex: changeFySpeedIndex,
      zySpeedIndex: index,
    })
  },

  changeFy(e){
    var index = e.currentTarget.dataset.index;
    let that = this;
    var zySpeedIndex = that.data.zySpeedIndex;
    var changeZySpeedIndex = that.data.zySpeedIndex;
    switch (index) {
      case 0:
        if(zySpeedIndex >= 1){
          changeZySpeedIndex = 0;
        }
        break;
      case 1:
        if(zySpeedIndex == 2){
          changeZySpeedIndex = 1;
        }
        break;
      case 2:
        break;
      default:
        break;
    }
    that.setData({
      zySpeedIndex: changeZySpeedIndex,
      fySpeedIndex: index
    })
  },
  
  mytouchstart: function(e){    
    let that = this;
    that.setData({
      touchStartTime: e.timeStamp
    })
    console.log(e.timeStamp + '- touch start') 
  },
    // //长按事件
    // mylongtap: function(e){ 
    //   let that = this;
    //   console.log((e.timeStamp - that.data.touchStartTime) + '- long tap') ;
    // },

    mytouchend: function(e){ 
        console.log(e.timeStamp + '- touch end') 
        let that = this;
        if(e.timeStamp - that.data.touchStartTime >= 5000){
          console.log('长按了5秒及以上');
          // that.setData({
          //   touchStartTime:0
          // })
          if(that.judgeBleConnect()){
            wx.navigateTo({
              url: '../debug/debug',
            })
          }
        }
    },

    showError(e) {
      //展示故障文字
      //处理故障码
      let that = this;
      // console.log(that.data.err)
      // that.setData({
      //   err: "401"
      // })
      var decimal = parseInt(that.data.err, 16); // 将十六进制转换为十进制
      var binary = decimal.toString(2)
      if(binary.length < 12){
        var index = 12-binary.length;
        for(var i = 0;i < index;i++){
          binary = "0" + binary
        }
      }
      var errArr = Array()
      for(var i = 0;i<binary.length;i++){
        var item = binary.substring(i, i+1);
        if(item == "1"){
          var errStr = "";
          switch (i) {
            case 0:
              errStr = "副翼霍尔故障";
              break;
            case 1:
              errStr = "主翼霍尔故障";
              break;
            case 2:
              errStr = "副翼电流检测出错";
              break;
            case 3:
              errStr = "主翼电流检测出错";
              break;
            case 4:
              errStr = "尾翼自学习失败";
              break;
            case 5:
              errStr = "尾翼速度参数保存失败";
              break;
            case 6:
              errStr = "尾翼速度参数无效";
              break;
            case 7:
              errStr = "欠压";
              break;
            case 8:
              errStr = "副翼关闭失败";
              break;
            case 9:
              errStr = "副翼打开失败";
              break;
            case 10:
              errStr = "主翼关闭失败";
              break;
            case 11:
              errStr = "主翼打开失败";
              break;
          }
          errArr.push(errStr)
        }
      }

      that.setData({
        errArr: errArr,
        modalName: "DialogModal1"
      })
    },

    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
 
    judgeBleConnect(){
      var BLEData = app.globalData.bleTool.getBLEData();
      console.log(BLEData.connectedDevice);
      if(BLEData.connectedDevice == null){
        wx.showToast({
          title: '请先连接蓝牙设备',
          icon:'none'
        })
        return false;
      }
      return true;
    },

    //蓝牙回调
    disconnect(){
        let that = this;
        that.setData({
            isBleConnect: false
        })
        wx.showToast({
          title: '蓝牙已断开',
          icon: "error"
        })
    },

    haveConnected(){
        console.log("我连上了");
        let that = this;
        that.setData({
            isBleConnect: true,
            bleName: app.globalData.bleTool.getBLEData().connectedDevice.name
        })
        var order = "info:?\r\n";
        app.globalData.bleTool.writeBLECharacteristicValue(order);
    },
})
